
<html><HEAD>
<LINK REL=STYLESHEET HREF="default.css" TYPE="text/css">
<TITLE>
About crosstabs </TITLE>
</HEAD>
<BODY>

<!-- Header -->
<p class="ancestor" align="right"><A HREF="pbugp230.htm">Previous</A>&nbsp;&nbsp;<A HREF="pbugp232.htm" >Next</A>
<!-- End Header -->
<A NAME="X-REF352128827"></A><h1>About crosstabs </h1>
<A NAME="TI8035"></A><p>Cross tabulation is a useful technique for analyzing data.
By presenting data in a spreadsheet-like grid, a crosstab lets users view
summary data instead of a long series of rows and columns. For example,
in a sales application you might want to summarize the quarterly
unit sales of each product.</p>
<A NAME="TI8036"></A><p>In PowerBuilder, you create crosstabs by using the Crosstab
presentation style. When data is retrieved into the DataWindow object,
the crosstab processes all the data and presents the summary information
you have defined for it.</p>
<A NAME="TI8037"></A><h4>An example</h4>
<A NAME="TI8038"></A><p>Crosstabs are easiest to understand through an example. Consider the <b>Printer</b> table
in the EAS Demo DB. It records quarterly unit sales of printers
made by sales representatives in one year. (This is the same data used
to illustrate graphs in <A HREF="pbugp224.htm#CCJCJJAE">Chapter 26, "Working with Graphs ."</A>)</p>
<A NAME="TI8039"></A><table cellspacing=0 cellpadding=6 border=1 frame="void" rules="all"><caption>Table 27-1: The Printer table in the <ACRONYM title = "E A S" >EAS </ACRONYM>Demo
DB</caption>
<tr><th  rowspan="1"  ><A NAME="TI8040"></A>Rep</th>
<th  rowspan="1"  ><A NAME="TI8041"></A>Quarter</th>
<th  rowspan="1"  ><A NAME="TI8042"></A>Product</th>
<th  rowspan="1"  ><A NAME="TI8043"></A>Units</th>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8044"></A>Simpson</td>
<td  rowspan="1"  ><A NAME="TI8045"></A>Q1</td>
<td  rowspan="1"  ><A NAME="TI8046"></A>Stellar</td>
<td  rowspan="1"  ><A NAME="TI8047"></A>12</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8048"></A>Jones</td>
<td  rowspan="1"  ><A NAME="TI8049"></A>Q1</td>
<td  rowspan="1"  ><A NAME="TI8050"></A>Stellar</td>
<td  rowspan="1"  ><A NAME="TI8051"></A>18</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8052"></A>Perez</td>
<td  rowspan="1"  ><A NAME="TI8053"></A>Q1</td>
<td  rowspan="1"  ><A NAME="TI8054"></A>Stellar</td>
<td  rowspan="1"  ><A NAME="TI8055"></A>15</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8056"></A>Simpson</td>
<td  rowspan="1"  ><A NAME="TI8057"></A>Q1</td>
<td  rowspan="1"  ><A NAME="TI8058"></A>Cosmic</td>
<td  rowspan="1"  ><A NAME="TI8059"></A>33</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8060"></A>Jones</td>
<td  rowspan="1"  ><A NAME="TI8061"></A>Q1</td>
<td  rowspan="1"  ><A NAME="TI8062"></A>Cosmic</td>
<td  rowspan="1"  ><A NAME="TI8063"></A>5</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8064"></A>Perez</td>
<td  rowspan="1"  ><A NAME="TI8065"></A>Q1</td>
<td  rowspan="1"  ><A NAME="TI8066"></A>Cosmic</td>
<td  rowspan="1"  ><A NAME="TI8067"></A>26</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8068"></A>Simpson</td>
<td  rowspan="1"  ><A NAME="TI8069"></A>Q1</td>
<td  rowspan="1"  ><A NAME="TI8070"></A>Galactic</td>
<td  rowspan="1"  ><A NAME="TI8071"></A>6</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8072"></A>Jones</td>
<td  rowspan="1"  ><A NAME="TI8073"></A>Q1</td>
<td  rowspan="1"  ><A NAME="TI8074"></A>Galactic</td>
<td  rowspan="1"  ><A NAME="TI8075"></A>2</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8076"></A>Perez</td>
<td  rowspan="1"  ><A NAME="TI8077"></A>Q1</td>
<td  rowspan="1"  ><A NAME="TI8078"></A>Galactic</td>
<td  rowspan="1"  ><A NAME="TI8079"></A>1</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8080"></A>.</td>
<td  rowspan="1"  ><A NAME="TI8081"></A>.</td>
<td  rowspan="1"  ><A NAME="TI8082"></A>.</td>
<td  rowspan="1"  ><A NAME="TI8083"></A>.</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8084"></A>.</td>
<td  rowspan="1"  ><A NAME="TI8085"></A>.</td>
<td  rowspan="1"  ><A NAME="TI8086"></A>.</td>
<td  rowspan="1"  ><A NAME="TI8087"></A>.</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8088"></A>.</td>
<td  rowspan="1"  ><A NAME="TI8089"></A>.</td>
<td  rowspan="1"  ><A NAME="TI8090"></A>.</td>
<td  rowspan="1"  ><A NAME="TI8091"></A>.</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8092"></A>Simpson</td>
<td  rowspan="1"  ><A NAME="TI8093"></A>Q4</td>
<td  rowspan="1"  ><A NAME="TI8094"></A>Stellar</td>
<td  rowspan="1"  ><A NAME="TI8095"></A>30</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8096"></A>Jones</td>
<td  rowspan="1"  ><A NAME="TI8097"></A>Q4</td>
<td  rowspan="1"  ><A NAME="TI8098"></A>Stellar</td>
<td  rowspan="1"  ><A NAME="TI8099"></A>24</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8100"></A>Perez</td>
<td  rowspan="1"  ><A NAME="TI8101"></A>Q4</td>
<td  rowspan="1"  ><A NAME="TI8102"></A>Stellar</td>
<td  rowspan="1"  ><A NAME="TI8103"></A>36</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8104"></A>Simpson</td>
<td  rowspan="1"  ><A NAME="TI8105"></A>Q4</td>
<td  rowspan="1"  ><A NAME="TI8106"></A>Cosmic</td>
<td  rowspan="1"  ><A NAME="TI8107"></A>60</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8108"></A>Jones</td>
<td  rowspan="1"  ><A NAME="TI8109"></A>Q4</td>
<td  rowspan="1"  ><A NAME="TI8110"></A>Cosmic</td>
<td  rowspan="1"  ><A NAME="TI8111"></A>52</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8112"></A>Perez</td>
<td  rowspan="1"  ><A NAME="TI8113"></A>Q4</td>
<td  rowspan="1"  ><A NAME="TI8114"></A>Cosmic</td>
<td  rowspan="1"  ><A NAME="TI8115"></A>48</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8116"></A>Simpson</td>
<td  rowspan="1"  ><A NAME="TI8117"></A>Q4</td>
<td  rowspan="1"  ><A NAME="TI8118"></A>Galactic</td>
<td  rowspan="1"  ><A NAME="TI8119"></A>3</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8120"></A>Jones</td>
<td  rowspan="1"  ><A NAME="TI8121"></A>Q4</td>
<td  rowspan="1"  ><A NAME="TI8122"></A>Galactic</td>
<td  rowspan="1"  ><A NAME="TI8123"></A>3</td>
</tr>
<tr><td  rowspan="1"  ><A NAME="TI8124"></A>Perez</td>
<td  rowspan="1"  ><A NAME="TI8125"></A>Q4</td>
<td  rowspan="1"  ><A NAME="TI8126"></A>Galactic</td>
<td  rowspan="1"  ><A NAME="TI8127"></A>6</td>
</tr>
</table>
<A NAME="TI8128"></A><p>This information can be summarized in a crosstab. Here is
a crosstab that shows unit sales by printer for each quarter:</p>
<br><img src="images/cros02.gif">
<A NAME="TI8129"></A><p>The first-quarter sales of Cosmic printers displays in the
first data cell. (As you can see from the data in the <b>Printer</b> table
shown before the crosstab, in Q1 Simpson sold 33 units, Jones sold
5 units, and Perez sold 26 units&#8212;totaling 64 units.) PowerBuilder calculates
each of the other data cells the same way.</p>
<A NAME="TI8130"></A><p>To create this crosstab, you only have to tell PowerBuilder which
database columns contain the raw data for the crosstab, and PowerBuilder does
all the data summarization automatically.</p>
<A NAME="TI8131"></A><h4>What crosstabs do</h4>
<A NAME="TI8132"></A><p>Crosstabs perform two-dimensional analysis:<A NAME="TI8133"></A>
<ul>
<li class=fi>The first dimension is displayed as columns across
the crosstab.<br>
In the preceding crosstab, the first dimension is the quarter,
whose values are in the Quarter column in the database table.<br></li>
<li class=ds>The second dimension is displayed as rows down the
crosstab.<br>
In the preceding crosstab, the second dimension is the type
of printer, whose values are in the Product column in the database
table.<br>
</li>
</ul>
</p>
<A NAME="TI8134"></A><p>Each cell in a crosstab is the intersection of a column (the
first dimension) and a row (the second dimension). The numbers that
appear in the cells are calculations based on both dimensions. In
the preceding crosstab, it is the sum of unit sales for the quarter
in the corresponding column and printer in the corresponding row.</p>
<A NAME="TI8135"></A><p>Crosstabs also include summary statistics. The preceding crosstab
totals the sales for each quarter in the last row and the total
sales for each printer in the last column.</p>
<A NAME="TI8136"></A><h4>How crosstabs are implemented in PowerBuilder</h4>
<A NAME="TI8137"></A><p>Crosstabs in PowerBuilder are implemented as grid DataWindow objects. Because
crosstabs are grid DataWindow objects, users can resize and reorder columns
at runtime (if you let them).</p>
<p><img src="images/note.gif" width=17 height=17 border=0 align="bottom" alt="Note"> <span class=shaded>Import methods return empty result</span> <A NAME="TI8138"></A>A crosstab report takes the original result set that was retrieved
from the database, sorts it, summarizes it, and generates a new
summary result set to fit the design of the crosstab. The <b>ImportFile</b>, <b>ImportClipboard</b>,
and <b>ImportString</b> methods can handle only the
original result set, and they return an empty result when used with
a crosstab report.</p>
<A NAME="TI8139"></A><h2>Two types of crosstabs</h2>
<A NAME="TI8140"></A><p>There are two types of crosstabs:<A NAME="TI8141"></A>
<ul>
<li class=fi>Dynamic</li>
<li class=ds>Static
</li>
</ul>
</p>
<A NAME="TI8142"></A><h4>Dynamic crosstabs</h4>
<A NAME="TI8143"></A><p>With dynamic crosstabs, PowerBuilder builds all the columns
and rows in the crosstab dynamically when you run the crosstab.
The number of columns and rows in the crosstab match the data that
exists at runtime.</p>
<A NAME="TI8144"></A><p>Using the preceding crosstab as an example, if a new printer
was added to the database after the crosstab was saved, there would
be an additional row in the crosstab when it is run. Similarly,
if one of the quarter's results was deleted from the database
after the crosstab was saved, there would be one less column in
the crosstab when it is run.</p>
<A NAME="TI8145"></A><p>By default, crosstabs you build are dynamic.</p>
<A NAME="TI8146"></A><h4>Static crosstabs</h4>
<A NAME="TI8147"></A><p>Static crosstabs are quite different from dynamic crosstabs. With static crosstabs, PowerBuilder establishes
the columns in the crosstab based on the data in the database when
you <i>define</i> the crosstab. (It does this by retrieving data
from the database when you initially define the crosstab.) No matter
what values are in the database when you later run the crosstab,
the crosstab always has the same columns as when you defined it.</p>
<A NAME="TI8148"></A><p>Using the preceding crosstab as an example, if there were
four quarters in the database when you defined and saved the crosstab,
there would always be four columns (Q1, Q2, Q3, and Q4) in the crosstab
at runtime, even if the number of columns changed in the database.</p>
<A NAME="TI8149"></A><h4>Advantages of dynamic crosstabs</h4>
<A NAME="TI8150"></A><p>Dynamic crosstabs are used more often than static crosstabs,
for the following reasons:<A NAME="TI8151"></A>
<ul>
<li class=fi>You
can define dynamic crosstabs very quickly because no database access
is required at definition time.</li>
<li class=ds>Dynamic crosstabs always use the current data to
build the columns and rows in the crosstab. Static crosstabs show
a snapshot of columns as they were when the crosstab was defined.</li>
<li class=ds>Dynamic crosstabs are easy to modify: all properties
for the dynamically built columns are replicated at runtime automatically.
With static crosstabs, you must work with one column at a time. 
</li>
</ul>
</p>

